package memmap

import "sync"

type ObjectId uint64

var refs struct {
	sync.Mutex
	objs map[ObjectId]interface{}
	next ObjectId
}

func init() {
	refs.Lock()
	defer refs.Unlock()
	refs.objs = make(map[ObjectId]interface{})
	refs.next = 1000
}
func NewObjectId(obj interface{}) ObjectId {
	refs.Lock()
	defer refs.Unlock()
	id := refs.next
	refs.next++
	refs.objs[id] = obj
	return id
}
func (id ObjectId) IsNil() bool {
	return id == 0
}
func (id ObjectId) Get() interface{} {
	refs.Lock()
	defer refs.Unlock()
	return refs.objs[id]
}
func (id *ObjectId) Free() interface{} {
	refs.Lock()
	defer refs.Unlock()
	obj := refs.objs[*id]
	delete(refs.objs, *id)
	*id = 0
	return obj
}
